package com.cn.kabalifemerchants.ui.user.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.ui.login.bean.AddressProvinceListBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.page.bean.UpImgBean;
import com.cn.kabalifemerchants.ui.user.bean.MyUserInfoBean;
import com.cn.kabalifemerchants.utils.GlideEngine;
import com.cn.kabalifemerchants.utils.GlideUtil;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.cn.kabalifemerchants.utils.RxToast;
import com.kongzue.dialog.listener.OnMenuItemClickListener;
import com.kongzue.dialog.v2.BottomMenu;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/10/21.
 * function: 上传封面
 */
public class CoverImgActivity extends IBaseActivity<UserInfoView, UserInfoPresenter> implements UserInfoView {
    @BindView(R.id.img_cover)
    ImageView imgCover;


    public final static int CHOOSE_PHOTO_1 = 1001;
    private List<LocalMedia> selectHead = new ArrayList<>();
    String pathHead = "";//头像地址
    private int themeId;//
    private String[] choosePhotos;

    Bundle bundle;
    String img;
    String Upimgstr;//上传返回的

    @Override
    protected UserInfoPresenter createPresenter() {
        return new UserInfoPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cover_img;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        setTitleBar("上传封面");
        bundle = getIntent().getExtras();
        img = bundle.getString("img");
        if (!TextUtils.isEmpty(img))
            GlideUtil.ImageLoad(this, img, imgCover);
        choosePhotos = new String[]{getResources().getString(R.string.main_img_photograph), getResources().getString(R.string.main_img_album)};
        ///样式
        themeId = R.style.picture_default_style;
    }

    @OnClick({R.id.img_cover, R.id.tv_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_cover:
                showBottomMenu(choosePhotos);
                break;
            case R.id.tv_btn_next:
                if (!TextUtils.isEmpty(Upimgstr))
                    mPresenter.onShopCoverUpData(LoginHelper.getLonginData().getResult(), Upimgstr);
                break;
        }
    }

    /**
     * 展示底部菜单选择
     *
     * @param strings 数组
     */
    private void showBottomMenu(String[] strings) {
        BottomMenu.show(this, strings, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                switch (index) {
                    case 1://TODO 相册
                        PictureSelector.create(CoverImgActivity.this)
                                .openGallery(PictureMimeType.ofImage())
                                .loadImageEngine(GlideEngine.createGlideEngine())
                                .theme(themeId)
                                .selectionMode(PictureConfig.SINGLE)
                                .imageSpanCount(4)
                                .previewImage(true)
                                .isCamera(true)
                                .enableCrop(true)// 是否裁剪
                                .compress(true)// 是否压缩
                                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                                .scaleEnabled(true)
                                .withAspectRatio(18, 18)
                                .minimumCompressSize(100)// 小于100kb的图片不压缩
                                .forResult(CHOOSE_PHOTO_1);


                        break;
                    case 0://TODO 相机
                        // 单独拍照
                        PictureSelector.create(CoverImgActivity.this)
                                .openCamera(PictureMimeType.ofImage())// 单独拍照，也可录像或也可音频 看你传入的类型是图片or视频
                                .loadImageEngine(GlideEngine.createGlideEngine())
                                .theme(themeId)
                                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                                .previewImage(true)// 是否可预览图片
                                .isCamera(true)// 是否显示拍照按钮
                                .enableCrop(true)// 是否裁剪
                                .compress(true)// 是否压缩
                                .scaleEnabled(true)
                                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                                .withAspectRatio(18, 18)
                                .openClickSound(true)// 是否开启点击声音
                                .previewEggs(false)//预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中)
                                .minimumCompressSize(100)// 小于100kb的图片不压缩
                                .forResult(CHOOSE_PHOTO_1);//结果回调onActivityResult code
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
//            case onActivityResult_To_ComName:
//                if (data != null) {
//                    String name = data.getStringExtra("name");
//                    tvName.setText(name);
////                    String time = TimeUtils.getTime10();
////                    mPresenter.onUpUserInfoData(time, 1, name, LoginHelper.getLonginData().getInfo().getUuid());
//                }
//                break;
            case RESULT_OK:
                switch (requestCode) {
                    case CHOOSE_PHOTO_1:
                        // 图片选择结果回调
                        selectHead = PictureSelector.obtainMultipleResult(data);
                        // 例如 LocalMedia 里面返回三种path
                        // 1.media.getPath(); 为原图path
                        // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                        // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                        // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的

                        for (LocalMedia media : selectHead) {
                            Log.i("图片-----》", media.getPath());

                            if (media.isCut() && !media.isCompressed()) {
                                // 裁剪过
                                pathHead = media.getCutPath();
                            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                                pathHead = media.getCompressPath();
                            } else {
                                // 原图
                                pathHead = media.getPath();
                            }
                            LoggerUtils.d("图片的地址=" + pathHead);
                            Glide.with(this).load(pathHead).into(imgCover);
                            //todo 上传头像
                            mPresenter.onUpImgData("head", pathHead);
                        }
                        break;
                }
            default:
                break;

        }
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onUpImgSuccess(UpImgBean bean) {
        Upimgstr = bean.getResult().get(0);
    }

    @Override
    public void onUpUserInfoSuccess(MsgBean bean) {

    }

    @Override
    public void onUserInfoSuccess(MyUserInfoBean bean) {

    }

    @Override
    public void onUserOutSuccess(MsgBean bean) {

    }

    @Override
    public void onShopUpSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        finish();
    }

    @Override
    public void onProvinceListSuccess(AddressProvinceListBean bean) {

    }

    @Override
    public void onCityListSuccess(AddressProvinceListBean bean) {

    }
}
